package com.cm.etax.tianjin;

import com.cm.etax.BaseEtax;
import com.cm.etax.BaseFunction;
import com.cm.etax.FunctionResult;
import com.cm.etax.exceptions.EtaxException;
import com.cm.etax.tianjin.bean.SysApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询绑定企业 列表
 *
 * @author LWang 2023.01.30
 * @since 1.0.0
 */
@Slf4j
public class QueryBindEnterpriseFunction extends BaseFunction {

    public QueryBindEnterpriseFunction(BaseEtax etax) {
        super(etax);
    }


    public FunctionResult<List<String>> queryBindEnterprise() {
        // 切换到 用户中心 - 自然人账户中心
        var menuFunctionObj = etax().function(MenuFunction.class);
        var choiceMainMenuResult = menuFunctionObj.choiceMainMenu("用户中心", "自然人账户中心");
        if (!choiceMainMenuResult.success()) {
            return new FunctionResult<>(false, choiceMainMenuResult.message(), null);
        }
        var choiceAccountCenterMenuResult = menuFunctionObj.choiceAccountCenterMenu("企业授权管理", "已授权企业");
        if (!choiceAccountCenterMenuResult.success()) {
            return new FunctionResult<>(false, choiceAccountCenterMenuResult.message(), null);
        }
        try {
            rpa().findElementWithInnerText("已授权企业", By.cssSelector("div.s_Breadcrumb"), WAIT_TIME);
            var list = new ArrayList<String>();
            // 每页长度改为 40
            var page = rpa().findElement(By.cssSelector(".el-pagination .el-pagination__sizes .el-select"), WAIT_TIME);
            rpa().click(page);
            rpa().newHar();
            rpa().click(rpa().findElementWithInnerText("40条/页", By.cssSelector(".el-select-dropdown__wrap .el-select-dropdown__item span"), WAIT_TIME));
            var response = rpa().jsonListen("idm/internal/relation/selectRelationList", SysApiResponse.class);
            if (!response.success()) {
                throw new EtaxException(response.msg());
            }
            // 查询列表
            while (true) {
                var rows = rpa().findElements(By.cssSelector(".el-table__body-wrapper .el-table__row"), WAIT_TIME);
                for (var row : rows) {
                    var cells = row.findElements(By.tagName("td"));
                    var line = new ArrayList<String>(cells.size());
                    for (var cell : cells) {
                        line.add(cell.getText());
                    }
                    list.add(StringUtils.join(line, "\t"));
                }
                // 读取下一页按钮
                var nextPage = rpa().findElement(By.cssSelector(".btn-next"), WAIT_TIME);
                if (!nextPage.isEnabled()) {
                    break;
                }
                rpa().newHar();
                nextPage.click();
                response = rpa().jsonListen("idm/internal/relation/selectRelationList", SysApiResponse.class);
                if (!response.success()) {
                    throw new EtaxException(response.msg());
                }
                rpa().sleep(1000);
            }
            return new FunctionResult<>(true, "OK", list);
        } catch (Exception e) {
            return handleException(e, "查询绑定企业失败，");
        }
    }
}
